<?php
require_once('includes/config.php');
include('includes/sc-includes.php');

$pagetitle = ContactDetails;

//
// Get values from our query string
//
$update = 0;
if (isset($_GET['note']))
{
	$update = 1;
	$note_id = $_GET['note'];
}

$contact_id = $_GET['id'];

//
// Get values for our contact
//
$row_contact = $sdb->getAttributes('contacts', $contact_id);

//
// Get notes that match this contact
//
$row_notes = $sdb->select('notes', "SELECT * from notes WHERE contact_id = '$contact_id'");

//
// Insert note for contact
//
if ($update == 0)
{
	if ($_POST['note_text'])
	{
		//
		// Add our note
		//
		$note_id = uniqid();
		$note_text = addslashes($_POST['note_text']);

		$attributes =  array(
							 "contact_id" 	=> array("value" => $contact_id, "replace" 	=> "true"),
							 "note_text" 	=> array("value" => $note_text, "replace" 	=> "true"),
							 "note_date" 	=> array("value" => time(), 	"replace" 	=> "true")
							);

		$sdb->putAttributes('notes', $note_id, $attributes);

		set_msg('Note Added');

		$goto = "contact-details.php?id=$contact_id";
		header(sprintf('Location: %s', $goto)); die;
	}
}
//
// Update an existing note
//
else if ($update == 1)
{
	if ($_POST['note_text'])
	{
		//
		// Figure out which note attribute we want to update
		//
		$attributes =  array("note_text" => array("value" => addslashes($_POST['note_text']), "replace" => "true"));
		$sdb->putAttributes('notes', $note_id, $attributes);

		$goto = "contact-details.php?id=$contact_id";
		set_msg('Note Updated');

		header(sprintf('Location: %s', $goto)); die;
	}
	else
	{
		//
		// We are updating, but we don't have a new note value yet
		//
		$update_note_text = $sdb->getAttributes("notes", $note_id);
		$update_note_text = $update_note_text['note_text'];
	}
}

//
// Update history
//
$query_checkhistory = "SELECT history_contact FROM history WHERE history_contact = '$id'";
$row_checkhistory = $sdb->select('history', $query_checkhistory);
$totalRows_checkhistory = count($row_checkhistory);

if ($totalRows_checkhistory > 0)
{
	$attributes =  array("history_status" 	=> array("value" => "2", "replace" => "true"));
	$sdb->putAttributes('history', $contact_id, $attributes);
}

$history_id = uniqid();
$history_date = time();
$history_status = 1;

$attributes =  array("history_contact" 	=> array("value" => $contact_id, "replace" => "true"),
					 "history_date"  	=> array("value" => time(), 	"replace" => "true"),
				 	 "history_status" 	=> array("value" => "1", 		"replace" => "true"));

$sdb->putAttributes('history', $history_id, $attributes);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title><?php echo $row_contact['contact_first']; ?> <?php echo $row_contact['contact_last']; ?></title>
	<script src="includes/lib/prototype.js" type="text/javascript"></script>
	<script src="includes/src/effects.js" type="text/javascript"></script>
	<script src="includes/validation.js" type="text/javascript"></script>
	<script src="includes/src/scriptaculous.js" type="text/javascript"></script>
	<script src="includes/src/unittest.js" type="text/javascript"></script>
	<link href="includes/style.css" rel="stylesheet" type="text/css" />
	<link href="includes/simplecustomer.css" rel="stylesheet" type="text/css" />
</head>

<body <?php if ($row_notes['note_date'] > time()-1) { ?>onload="new Effect.Highlight('newnote'); return false;"<?php } ?>>
	<?php include('includes/header.php'); ?>
	<div class="container">
  		<div class="leftcolumn">
			<span class="notices" style="display:<?php echo $dis; ?>">
    			<?php display_msg(); ?>
    		</span>
			<div style="display:block; margin-bottom:5px">
				<?php if ($row_contact['contact_image']) { ?><img src="images/<?php echo $row_contact['contact_image']; ?>" width="95" height="71" class="contactimage" /><?php } ?>
				<h2>
					<?php echo $row_contact['contact_first']; ?>
					<?php echo $row_contact['contact_last']; ?>
					<?php if ($row_contact['contact_company'])
						  { ?>
					<span style="color:#999999"> with <?php echo $row_contact['contact_company']; ?></span>
					<?php } ?>
					<a style="font-size:12px; font-weight:normal" href="contact.php?id=<?php echo $row_contact['contact_id']; ?>">&nbsp;&nbsp;+ Edit contact </a>
				</h2>
				<br clear="all" />
			</div>
			<p><br/></p>
    		<form id="form1" name="form1" method="post" action="">
				<?php if ($update == 0) { echo "Add a new note <br>"; } ?>
				<textarea name="note_text" style="width:95% "rows="3" id="note_text" class="required"><?php echo $update_note_text; ?></textarea>
				<input type="hidden" name="note" id="note" value="<?php print $note_id;?>"/>
				<br />
				<input type="submit" name="Submit2" value="<?php if ($update == 1) { echo Update; } else { echo Add; } ?> note" />
				<?php if ($update == 1) { ?>  <a href="delete.php?note=<?php echo $_GET['note']; ?>&amp;id=<?php echo $contact_id ?>" onclick="javascript:return confirm('Are you sure you want to delete this note?')">Delete Note</a><?php } ?>
				<?php
					if (count($row_notes) > 0)
					{
				?>
						<hr/>
						<?php
				    	//
				    	// List out our notes
				    	//
				    	foreach ($row_notes as $note)
						{
							$note_date = $note['Attributes']['note_date'];
							$note_text = $note['Attributes']['note_text'];
							$note_id = $note['Name'];
						?>
						<div>
							<span class="datedisplay">
								<a href="?id=<?php echo $row_contact['contact_id']; ?>&note=<?php echo $note_id; ?>">
									<?php echo date('F d, Y', $note_date); ?>
								</a>
							</span>
							<br/>
							<?php echo $note_text; ?>
						</div>
						<hr/>
						<?php
		    			}
					}
						?>
			</form>
    		<p>&nbsp;</p>
    		<p>&nbsp;</p>
  		</div>
  		<?php include('includes/right-column.php'); ?>
  	<br clear="all" />
</div>
<?php include('includes/footer.php'); ?>
</body>
</html>
